# This is a sample Python script.
import os
import time

import common

# 对文件夹下所有的 指定类型的文件在末尾追加注释
if __name__ == '__main__':
    path = common.open_dir()
    file_pattern = common.show_input_dialog('title', '需要查找的文件类型，多个以,号分割')

    pattern = []
    for value in file_pattern.split(','):
        pattern.append('.' + value)
    file_map_list = common.get_file_map(path, pattern)

    start_time = int(time.time())
    nums = 0  # 文件数量

    # 读取注释模板文件
    file_map = common.get_template_map(os.getcwd() + os.sep + 'template', [])
    for key, value in file_map_list.items():
        print(key)
        with open(file_map.get(key), encoding='utf-8') as template_file:
            file_read = template_file.read()
        for index, file_name in enumerate(value, 1):
            nums += 1
            # print(file_name)
            count = len(open(file_name, encoding='utf-8').readlines())
            num = count / 200 + 1
            # 根据比率增加
            with open(file_name, 'a+', encoding='utf-8') as f:
                f.write('\n')
                for ind in range(0, int(num)):
                    f.write(file_read + '\n')
    end_time = int(time.time())
    print('文件总数：%i; 总用时：%i' % (nums, end_time - start_time))
